package com.xbongbong.paas.service.stage;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.domain.entity.PaasStageEntity;
import com.xbongbong.paas.domain.entity.StageProcessEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.model.StageProcessModel;
import com.xbongbong.paas.pojo.FieldAttrEntityForImport;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description 阶段抽象方法
 * @Author sunshaoxiang
 * @Date 2021/11/28 10:59
 * @Version 1.0
 */
@Component
public abstract class AbstractStageHandleImpl implements StageHandle {

    private static final Logger LOG = LoggerFactory.getLogger(AbstractStageHandleImpl.class);

    @Resource
    private StageProcessModel stageProcessModel;

    protected <T extends PaasStageEntity> List<ItemPoJo> updateStageInfoInExplain(List<T> paasStageEntities, Long formId, String corpid) throws XbbException {
        List<ItemPoJo> items = new ArrayList<>();
        try {
            for (T entity : paasStageEntities) {
                ItemPoJo itemPoJo = new ItemPoJo();
                itemPoJo.setValue(entity.getCode());
                if (CollectionsUtil.isNotEmpty(entity.getLinkOption())) {
                    itemPoJo.setFieldMap(JSON.parseArray(JSON.toJSONString(entity.getLinkOption()), String.class));
                }
                if (Objects.equals(entity.getIsDefault(), BasicConstant.ONE)) {
                    itemPoJo.setChecked(true);
                }
                itemPoJo.setText(entity.getName());
                items.add(itemPoJo);
            }
            return items;
        } catch (Exception e) {
            LOG.error("AbstractStageHandleImpl.updateStageInfoInExplain 出错",e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public void stageGetSettingAdvance(PaasStageEntity paasStageEntity, Object stageVO, Integer stageSetting) {
        // do nothing
    }

    @Override
    public void stageSaveSettingAdvance(PaasStageEntity paasStageEntity, Object stageSave, Integer stageSetting) {
        // do nothing
    }

    /**
     * 导入的时候 获取对应的阶段用于excel的下拉选择框
     *
     * @param corpid
     * @param formId
     * @param enable
     * @param stageField
     * @return
     */
    @Override
    public List<ItemPoJo> getItemListForImport(String corpid, Long formId, Integer enable, FieldAttrEntityForImport stageField) {
        List<ItemPoJo> itemPoJoList = new ArrayList<>();
        if (!Objects.equals(FieldTypeEnum.STAGE_THRUSTER.getType(), stageField.getFieldType())) {
            return itemPoJoList;
        }
        StageProcessEntity stageProcessEntity;
        // 判断是否多版本启用，如果是多版本启用，那么获取默认的版本
        if (Objects.equals(stageField.getStageMultiVersion(), 1) && Objects.nonNull(stageField.getDefaultStageProcessId())) {
            stageProcessEntity = stageProcessModel.getByKey(stageField.getDefaultStageProcessId(), corpid);
        } else {
            stageProcessEntity = stageProcessModel.getEnableStageProcess(corpid, formId);
        }
        if (Objects.isNull(stageProcessEntity)) {
            return itemPoJoList;
        }
        Long stageProcessId = stageProcessEntity.getId();
        Map<String, Object> params = new HashMap<>();
        params.put(ParameterConstant.CORPID, corpid);
        params.put(ParameterConstant.FORMID, formId);
        params.put(ParameterConstant.STAGE_PROCESS_ID, stageProcessId);
        params.put(ParameterConstant.ENABLE, enable);
        params.put(ParameterConstant.DEL, BasicConstant.ZERO);
        params.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT + " " + SortOrder.DESC);
        List<PaasStageEntity> stageList = findEntitys(params);
        for (PaasStageEntity entity : stageList){
            ItemPoJo itemPoJo = new ItemPoJo();
            itemPoJo.setText(entity.getName());
            itemPoJo.setValue(entity.getCode());
            itemPoJo.setStageId(entity.getId());
            itemPoJo.setStageType(entity.getType());
            itemPoJo.setStageProcessId(entity.getStageProcessId());
            itemPoJo.setStageRatio(entity.getOutProportion());
            itemPoJo.setIsOther(BasicConstant.ZERO);
            if (Objects.equals(entity.getIsDefault(), BasicConstant.ONE)) {
                itemPoJo.setChecked(true);
            }
            if (CollectionsUtil.isNotEmpty(entity.getLinkOption())) {
                itemPoJo.setFieldMap(JSON.parseArray(JSON.toJSONString(entity.getLinkOption()), String.class));
            }
            itemPoJoList.add(itemPoJo);
        }
        return itemPoJoList;
    }

    @Override
    public List<ItemPoJo> getItemList(String corpid, Long formId, Integer enable) {
        List<ItemPoJo> itemPoJoList = new ArrayList<>();
        StageProcessEntity stageProcessEntity = stageProcessModel.getEnableStageProcess(corpid, formId);
        if (Objects.isNull(stageProcessEntity)) {
            return itemPoJoList;
        }
        Long stageProcessId = stageProcessEntity.getId();
        Map<String, Object> params = new HashMap<>();
        params.put(ParameterConstant.CORPID, corpid);
        params.put(ParameterConstant.FORMID, formId);
        params.put(ParameterConstant.STAGE_PROCESS_ID, stageProcessId);
        params.put(ParameterConstant.ENABLE, enable);
        params.put(ParameterConstant.DEL, BasicConstant.ZERO);
        params.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT + " " + SortOrder.DESC);
        List<PaasStageEntity> stageList = findEntitys(params);
        for (PaasStageEntity entity : stageList){
            ItemPoJo itemPoJo = new ItemPoJo();
            itemPoJo.setText(entity.getName());
            itemPoJo.setValue(entity.getCode());
            itemPoJo.setStageId(entity.getId());
            itemPoJo.setStageType(entity.getType());
            itemPoJo.setStageProcessId(entity.getStageProcessId());
            itemPoJo.setStageRatio(entity.getOutProportion());
            itemPoJo.setIsOther(BasicConstant.ZERO);
            if (Objects.equals(entity.getIsDefault(), BasicConstant.ONE)) {
                itemPoJo.setChecked(true);
            }
            if (CollectionsUtil.isNotEmpty(entity.getLinkOption())) {
                itemPoJo.setFieldMap(JSON.parseArray(JSON.toJSONString(entity.getLinkOption()), String.class));
            }
            itemPoJoList.add(itemPoJo);
        }
        return itemPoJoList;
    }
}
